package edu.zut.cs.software.cms.module.all.web.spring.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import edu.zut.cs.software.cms.base.web.spring.controller.GenericController;
import edu.zut.cs.software.cms.module.all.domain.All;
import edu.zut.cs.software.cms.module.all.service.AllManager;

@Controller
@RequestMapping("/all")
@ResponseBody
public class AllController extends GenericController<All, Long, AllManager> {
	AllManager allManager;

	@Autowired
	public void setAllManager(AllManager allManager) {
		this.allManager = allManager;
		this.manager = this.allManager;
	}
	/*
	 * @RequestMapping(method = RequestMethod.GET, value = "/index.html") public
	 * String index() { String result = "/all/index"; return result; }
	 */

//	@RequestMapping("/CreateOne")
//	@ResponseBody
//	public String CreateOne(String username,String password,String identity) {
//		String flag="";
//		All all=new All();
//		if(allManager.IfExist(username))//返回true
//		{
//			flag="true";
//			all.setUsername(username);
//			all.setPassword(password);
//			all.setIdentity(identity);
//			allManager.save(all);
//		}
//		else {
//			flag="false";
//		}
//		HashMap<String,Object> res=new HashMap<String, Object>();//放入map集合K:V
//		res.put("flag", flag);//收容标记flag 前是字符串后是字符串对象定义的hashmap
////		res.put("token",token);
//		String res_json=JSON.toJSONString(res);//把一个对象转换为json串:把收容有flag标记和student对象转变成json数据
//		return res_json;//含有两块信息回应给前端
//	}
//	
//	@RequestMapping("/ModifyById")
//	@ResponseBody
//	public String ModifyById(long id,String username,String password,String identity) {
//		All all=new All();
//		all=allManager.getAllById(id);
//		all.setUsername(username);
//		all.setPassword(password);
//		all.setIdentity(identity);
//		this.allManager.save(all);
//		return "true";
//	}
//	
//	@RequestMapping("/DeleteById")
//	@ResponseBody
//	public String DeleteById(long id) {
//		allManager.delete(id);
//		return "success!";
//	}
//	
//	@RequestMapping("/RetrieveById")
//	@ResponseBody
//	public All RetrieveById(long id) {
//		All all=new All();
//		all=allManager.findById(id);
//		return all;
//	}

	@ResponseBody
	@GetMapping(value = "findall", produces = "application/json;charset=utf-8")
	public String findAll() {
		List<All> allList = this.manager.getList();
		PageInfo pageInfo = new PageInfo(allList);
		return JSON.toJSONString(pageInfo);
	}

	@GetMapping("/findbyid/{id}")
	public String fingById(@PathVariable("id") Long id) {
		Optional<All> result = this.manager.findByAllId(id);
		return JSON.toJSONString(result);
	}

	@GetMapping("/deletebyid/{id}")
	public Boolean deleteById(@PathVariable("id") Long id) {
		Boolean result = this.manager.deleteById(id);
		return result;
	}

}